<template>
  <div class="app-container">
    <!-- 第一排搜索卡片 -->
    <el-card class="box-card">
      <el-row
        style="font-size: 15px; line-height: 2em; color: #909399"
        :gutter="60"
      >
        <el-col :span="6">
          <div class="two_left" style="padding-left: 30px; padding-right: 0px">
            <div>
              <el-button
                type="text"
                @click="handleEdit()"
                style="font-size: 30px; font-color: white"
                >添加学员</el-button
              >
            </div>
          </div>
        </el-col>
        <el-col :span="18">
          <el-form :model="studentFindByPage">
            <el-col :span="12">
              <div class="search">
                学员名字：<el-input
                  v-model="studentFindByPage.etmsUser.userName"
                  placeholder="不限"
                  style="width: 70%"
                ></el-input>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search">
                公司名称：<el-input
                  v-model="studentFindByPage.etmsUser.userCompony"
                  placeholder="不限"
                  style="width: 70%"
                ></el-input>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search">
                职位：{{ "\xa0\xa0\xa0\xa0"
                }}<el-input
                  v-model="studentFindByPage.etmsUser.userPosition"
                  placeholder="不限"
                  style="width: 70%"
                ></el-input>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search" style="margin-left: 75px; margin-top: 20px">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="getStudentListBySearch"
                  >搜索</el-button
                >
              </div>
            </el-col>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="学员姓名" :label-width="formLabelWidth">
          <el-input v-model="form.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-select v-model="form.deptId" placeholder="请选择">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位" :label-width="formLabelWidth">
          <el-input v-model="form.userPosition" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="form.userTelephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.userEmail" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经理" :label-width="formLabelWidth">
          <el-input v-model="form.userManager" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.userRole" placeholder="请选择">
            <el-option
              v-for="role in options2"
              :key="role.value"
              :label="role.label"
              :value="role.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 第二排展示 -->
    <el-tabs v-model="activeName" style="margin-top: 30px" type="border-card">
      <div>
        <el-table
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{ 'text-align': 'center' }"
          :cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="学员姓名">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleClick(scope.$index, scope.row)"
                >{{ scope.row.userName }}</el-button
              >
            </template>
          </el-table-column>
          <el-table-column label="公司">
            <template slot-scope="scope">
              <span>{{ scope.row.userCompany }}</span>
            </template>
          </el-table-column>
          <el-table-column label="职位">
            <template slot-scope="scope">
              <span>{{ scope.row.userPosition }}</span>
            </template>
          </el-table-column>
          <el-table-column label="角色">
            <template slot-scope="scope" >
              <span>{{ scope.row.userRole }}</span>
            </template>
          </el-table-column>
          <el-table-column label="参与的培训">
            <template slot-scope="scope">
              <span>{{ scope.row.trainPrinc }}</span>
            </template>
          </el-table-column>
          <el-table-column label="参与的课程">
            <template slot-scope="scope">
              <span>{{ scope.row.trainPrinc }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进行中培训">
            <template slot-scope="scope">
              <span>{{ scope.row.trainPrinc }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进行中课程">
            <template slot-scope="scope">
              <span>{{ scope.row.trainPrinc }}</span>
            </template>
          </el-table-column>
          <el-table-column label="学习总时长">
            <template slot-scope="scope">
              <span>{{ scope.row.trainPrinc }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                style="margin-left: 0px"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="margin-top: 30px; margin-bottom: 20px; text-align: center"
        >
        </el-pagination>
      </div>
    </el-tabs>
  </div>
</template>

<script>
import { findAllStudent } from "@/api/student";
import { deleteStudent } from "@/api/student";
import { addStudent } from "@/api/student";
import { updateStudent } from "@/api/student";

export default {
  data() {
    return {
      options1: [
        {
          value: "1",
          label: "行政部",
        },
        {
          value: "2",
          label: "财务部",
        },
        {
          value: "3",
          label: "市场部",
        },
        {
          value: "4",
          label: "人力资源部",
        },
        {
          value: "5",
          label: "研发部",
        },
        {
          value: "6",
          label: "法务部",
        },
      ],
      options2:[{
        value: "1",
        label: "普通学员",
      },
      {
        value: "2",
        label: "经理",
      },
      {
        value: "3",
        label: "管理员",
      },
      {
        value: "4",
        label: "超级管理员",
      }],
      title: "添加学员",
      trainForm: "",
      studentFindByPage: {
        page: 1,
        limit: 5,
        etmsUser: {
          userName: "",
          userCompany: "",
          userPosition: "",
        },
      },
      total: 0,
      number: 0,
      currentPage: 1,
      tableData: [],
      dialogFormVisible: false,
      form: {
        userId: null,
        userName: '',
        deptId: '',
        userPosition: '',
        userTelephone: '',
        userEmail: '',
        userManager: '',
        userRole: '',
      },
      roleId:"",
      roleValue:"",
      formLabelWidth: "120px",
    };
  },
  created() {
    /* 获取查询列表 */
    this.getStudentListBySearch();
    
  },

  methods: {
    
    handleClick(index, row) {
      this.$router.push({
        name: "studentDetails",
        query: {
          userId: row.userId,
        },
      });
    },
    getStudentListBySearch() {
      findAllStudent(this.studentFindByPage)
        .then((res) => {
          console.log(res);
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.currentPage = res.data.currentPage;
          //this.currentPage = res.data.page;
        })
        .catch((err) => {
          console.log(err);
          alert("查询失败");
        });
        
    },

    handleSizeChange(val) {
      this.studentFindByPage.limit = val;
      findAllStudent(this.studentFindByPage).then((res) => {
        console.log(res);
        this.tableData = res.data.list;
        this.currentPage = res.data.pageNum;
      });
    },
    handleCurrentChange(val) {
      this.studentFindByPage.page = val;
      findAllStudent(this.studentFindByPage).then((res) => {
        console.log(res);
        this.tableData = res.data.list;
        this.currentPage = res.data.pageNum;
      });
      console.log(`当前页: ${val}`);
    },

    handleEdit(index, row) {
      this.dialogFormVisible = true;
      console.log(row);
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.form.userId = row.userId;
        this.form.userName = row.userName;
        this.form.deptId = row.deptId;
        this.form.userPosition = row.userPosition;
        this.form.userTelephone = row.userTelephone;
        this.form.userEmail = row.userEmail;
        this.form.userManager = row.userManager;
        this.form.userRole = row.userRole;
      } else {
        this.title = "新增";
        this.form.userId = null;
        this.form.userName = '';
        this.form.deptId = '';
        this.form.userPosition = '';
        this.form.userTelephone = '';
        this.form.userEmail = '';
        this.form.userManager = '';
        this.form.userRole = '';
      }
    },

    handleDelete(index, row) {
      deleteStudent(row.userId)
        .then((res) => {
          alert(res.data);
        })
        .catch((err) => {
          alert(err);
        });
      this.getStudentListBySearch();
    },
    closeDialog() {
      this.editFormVisible = false;
    },
    submit() {
      if (this.form.userId == null) {
        addStudent(this.form)
          .then((res) => {
            alert(res.data);
            this.dialogFormVisible = false;
            this.getStudentListBySearch();
          })
          .catch((err) => {
            console.log(err);
            alert(err);
          });
      } else {
        updateStudent(this.form)
          .then((res) => {
            alert(res.data);
            this.dialogFormVisible = false;
            this.getStudentListBySearch();
          })
          .catch((err) => {
            console.log(err);
            alert(err);
          });
      }
    },
    
  },
};
</script>



<style scoped>
.el-input {
  width: 250px;
}

.box-item {
  width: 75%;
  float: right;
}

.field-label {
  vertical-align: middle;
}

.box-card {
  width: 98%;
  max-width: 100%;
  margin: 20px auto;
}

.search {
  margin-top: 10px;
  width: 100%;
  display: flex;
  font-size: 15px;
  line-height: 50px;
}
.two_left > div:first-child {
  width: 100%;
  height: 150px;
  font-size: 30px;
  color: #fff;
  position: relative;
  background-image: url(../../../img/2.jpg);
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
